using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
namespace MicroMsg.Scene
{
	public class NetSceneNewReg : NetSceneBaseEx<NewRegRequest, NewRegResponse, NewRegRequest.Builder>
	{
		private const string TAG = "NetSceneNewReg";
		public void doSceneSetPwdforMobileReg(string rawPwd, string nickName)
		{
			Log.smethod_4("NetSceneNewReg", "mobile register (3/3) , set nick and password =" + nickName);
			if (nickName == null || nickName.get_Length() <= 0)
			{
				nickName = "nick";
			}
			this.doSceneEx("", Util.getCutPasswordMD5(rawPwd), "", nickName, 0, "", NetSceneBindOpMobileForReg.mMobileNumber, "", "", 1, NetSceneBindOpMobileForReg.mTicket);
		}
		public void doScene()
		{
			Log.smethod_4("NetSceneNewReg", string.Concat(new string[]
			{
				"qq register (3/3) , qq =",
				NetSceneAuth.mAuthUserName,
				", reg nick = ",
				NetSceneGetUserName.mRegNickName,
				", rep username = ",
				NetSceneGetUserName.mRepUserName,
				", account  = ",
				SessionPackMgr.getAccount().getUsername()
			}));
			this.doSceneEx(NetSceneGetUserName.mRepUserName, NetSceneAuth.mAuthUserPwdMD5, NetSceneAuth.mAuthUserPwd2MD5, NetSceneGetUserName.mRegNickName, Util.stringToInt(NetSceneAuth.mAuthUserName), "", "", "", "", 0, SessionPackMgr.mAuthTicket2);
		}
		private void doSceneEx(string account, string password, string password2, string nickName, int bindUin, string bindEmail, string bindMobile, string imgSid, string imgCode, int regMode, string ticket)
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(32);
			this.mBuilder.UserName = Util.NullAsNil(account);
			this.mBuilder.Pwd = Util.NullAsNil(password);
			this.mBuilder.NickName = Util.NullAsNil(nickName);
			this.mBuilder.BindUin = (uint)bindUin;
			this.mBuilder.BindEmail = Util.NullAsNil(bindEmail);
			this.mBuilder.BindMobile = Util.NullAsNil(bindMobile);
			this.mBuilder.RegMode = (uint)regMode;
			this.mBuilder.SetTicket(Util.NullAsNil(ticket));
			this.mBuilder.Language = AccountMgr.GetCurrentLanguage();
			this.mBuilder.DLSrc = (uint)AppInfoHelper.ChannelId;
			this.mSessionPack.mCmdID = 32;
			this.mSessionPack.mConnectMode = 2;
			base.endBuilder();
		}
		protected override void onSuccess(NewRegRequest request, NewRegResponse response)
		{
			int uin = (int)response.Uin;
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			uint regMode = request.RegMode;
			if (uin != 0 && ret == RetConst.MM_OK)
			{
				Log.smethod_4("NetSceneNewReg", "new reg success. ");
				AccountMgr.onLogin(response.UserName);
				HostService.updateAuthBuiltinIP(response.BuiltinIPList);
				this.updateUserInfo(request, response);
				if (regMode == 0u)
				{
					EventCenter.postEvent(EventConst.ON_NETSCENE_NEWREG_SUCCESS, null, null);
				}
				else
				{
					if (regMode == 1u)
					{
						EventCenter.postEvent(EventConst.ON_NETSCENE_MOBILEREG_SUCCESS, null, null);
					}
				}
				EventCenter.postEvent(EventConst.ON_NETSCENE_AUTH_SUCCESS, null, null);
				return;
			}
			this.postFailedEvent(regMode, (int)ret);
		}
		protected override void onFailed(NewRegRequest request, NewRegResponse response)
		{
			uint regMode = request.RegMode;
			this.postFailedEvent(regMode, -800000);
		}
		private void postFailedEvent(uint newRegMode, int int_0)
		{
			Log.smethod_1("NetSceneNewReg", "new reg failed, ret " + int_0);
			if (newRegMode == 0u)
			{
				EventCenter.postEvent(EventConst.ON_NETSCENE_NEWREG_ERR, int_0, null);
				return;
			}
			if (newRegMode == 1u)
			{
				EventCenter.postEvent(EventConst.ON_NETSCENE_MOBILEREG_ERR, int_0, null);
			}
		}
		private void updateUserInfo(NewRegRequest request, NewRegResponse response)
		{
			Account curAccount = AccountMgr.getCurAccount();
			curAccount.bytesSessionkey = Util.StringToByteArray(response.SessionKey);
			curAccount.bytesServerID = SessionPackMgr.getSeverID();
			curAccount.strPwd = request.Pwd;
			curAccount.nUin = response.Uin;
			curAccount.strUsrName = response.UserName;
			curAccount.strBindEmail = response.BindEmail;
			curAccount.strBindMobile = request.BindMobile;
			curAccount.nBindQQ = request.BindUin;
			curAccount.nStatus = response.Status;
			curAccount.strOfficalNickName = response.OfficialNickName;
			curAccount.strOfficalUserName = response.OfficialUserName;
			curAccount.nPushMailStatus = (int)response.PushMailStatus;
			curAccount.strQQMicroBlog = response.QQMicroBlogUserName;
			curAccount.nNewUser = 1;
			curAccount.dbLastSessionKeyTimeStamp = Util.getNowSeconds();
			if (!AccountMgr.updateAccount())
			{
				Log.smethod_1("NetSceneNewReg", "add account failed");
			}
		}
	}
}
